<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 内存溢出排查与解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .step-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            font-weight: bold;
        }
        .command-box {
            background: #1a1a1a;
            color: #00ff00;
            font-family: 'Courier New', monospace;
            padding: 12px 16px;
            border-radius: 8px;
            margin: 8px 0;
            position: relative;
            overflow-x: auto;
        }
        .command-box::before {
            content: '$';
            color: #666;
            margin-right: 8px;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 60px 0;
        }
        .highlight-box {
            background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .fade-in-up {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center fade-in-up">
            <div class="mb-6">
                <i class="fas fa-database text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl font-bold mb-6">Redis 内存溢出排查与解决方案</h1>
            <p class="text-xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入剖析 Redis 内存管理机制，系统化排查内存溢出问题，提供专业的解决方案，确保高性能缓存服务的稳定运行
            </p>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-10 card-hover">
                <div class="flex items-start mb-6">
                    <i class="fas fa-exclamation-triangle text-3xl text-yellow-500 mr-4"></i>
                    <div>
                        <h2 class="text-2xl font-bold mb-4">为什么要关注 Redis 内存管理？</h2>
                        <p class="text-gray-700 leading-relaxed">
                            Redis 作为一种高性能的内存数据库，广泛应用于缓存、会话管理等场景。然而，由于其运行时数据完全存储在内存中，因此内存的使用情况直接影响 Redis 的稳定性。如果 Redis 出现内存溢出（OutOfMemory），可能会导致数据丢失、服务不可用等严重问题。
                        </p>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-8">
                    <div class="text-center">
                        <i class="fas fa-tachometer-alt text-4xl text-purple-600 mb-3"></i>
                        <h3 class="font-semibold mb-2">高性能</h3>
                        <p class="text-sm text-gray-600">内存存储确保极速响应</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-memory text-4xl text-purple-600 mb-3"></i>
                        <h3 class="font-semibold mb-2">内存敏感</h3>
                        <p class="text-sm text-gray-600">内存使用直接影响稳定性</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-4xl text-purple-600 mb-3"></i>
                        <h3 class="font-semibold mb-2">风险防控</h3>
                        <p class="text-sm text-gray-600">避免数据丢失和服务中断</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Diagnostic Process Visualization -->
    <section class="py-16 px-6 bg-gray-100">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12">
                <span class="text-gradient">内存问题诊断流程图</span>
            </h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <div class="mermaid">
                    graph TD
                        A[发现内存异常] --> B{检查内存配置}
                        B --> C[分析内存使用]
                        C --> D{内存碎片率高?}
                        D -->|是| E[处理内存碎片]
                        D -->|否| F[识别大数据对象]
                        F --> G{存在大对象?}
                        G -->|是| H[优化数据结构]
                        G -->|否| I[检查过期策略]
                        I --> J[优化淘汰机制]
                        E --> K[监控优化效果]
                        H --> K
                        J --> K
                        K --> L{问题解决?}
                        L -->|否| M[考虑扩容/集群]
                        L -->|是| N[建立监控预警]
                </div>
            </div>
        </div>
    </section>

    <!-- Troubleshooting Steps -->
    <section class="py-16 px-6">
        <div class="max-w-5xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12">排查步骤</h2>
            
            <!-- Step 1 -->
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8 card-hover">
                <div class="flex items-start">
                    <div class="step-number mr-6">1</div>
                    <div class="flex-1">
                        <h3 class="text-xl font-bold mb-4">检查 Redis 内存配置</h3>
                        <p class="text-gray-700 mb-4">
                            首先，检查 Redis 的内存配置，特别是 <code class="bg-gray-100 px-2 py-1 rounded">maxmemory</code> 参数。该参数决定了 Redis 使用的最大内存量。
                        </p>
                        <div class="command-box">CONFIG GET maxmemory</div>
                        <div class="highlight-box mt-4">
                            <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                            <span class="font-semibold">提示：</span>将查询结果与实际内存使用情况对比，判断是否接近限制值。
                        </div>
                    </div>
                </div>
            </div>

            <!-- Step 2 -->
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8 card-hover">
                <div class="flex items-start">
                    <div class="step-number mr-6">2</div>
                    <div class="flex-1">
                        <h3 class="text-xl font-bold mb-4">分析内存使用情况</h3>
                        <p class="text-gray-700 mb-4">
                            使用以下命令查看 Redis 当前的内存使用情况，特别关注关键指标：
                        </p>
                        <div class="command-box">INFO memory</div>
                        <div class="command-box">MEMORY STATS</div>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-6">
                            <div class="bg-gray-50 p-4 rounded-lg">
                                <h4 class="font-semibold mb-2"><i class="fas fa-chart-line text-purple-600 mr-2"></i>used_memory</h4>
                                <p class="text-sm text-gray-600">Redis 已使用的内存量</p>
                            </div>
                            <div class="bg-gray-50 p-4 rounded-lg">
                                <h4 class="font-semibold mb-2"><i class="fas fa-server text-purple-600 mr-2"></i>used_memory_rss</h4>
                                <p class="text-sm text-gray-600">操作系统实际分配的内存</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Step 3 -->
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8 card-hover">
                <div class="flex items-start">
                    <div class="step-number mr-6">3</div>
                    <div class="flex-1">
                        <h3 class="text-xl font-bold mb-4">识别大数据对象</h3>
                        <p class="text-gray-700 mb-4">
                            找出占用大量内存的键，分析内存消耗分布：
                        </p>
                        <div class="command-box">MEMORY USAGE &lt;key&gt;</div>
                        <div class="command-box">SCAN 0 MATCH * COUNT 100</div>
                        <p class="text-gray-600 mt-4 text-sm">
                            <i class="fas fa-info-circle mr-2"></i>
                            结合 SCAN 和 MEMORY USAGE 命令遍历数据库，找出内存消耗最大的对象。
                        </p>
                    </div>
                </div>
            </div>

            <!-- Step 4 -->
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8 card-hover">
                <div class="flex items-start">
                    <div class="step-number mr-6">4</div>
                    <div class="flex-1">
                        <h3 class="text-xl font-bold mb-4">查看内存碎片</h3>
                        <p class="text-gray-700 mb-4">
                            通过 <code class="bg-gray-100 px-2 py-1 rounded">mem_fragmentation_ratio</code> 查看内存碎片率：
                        </p>
                        <div class="bg-gradient-to-r from-purple-50 to-pink-50 p-6 rounded-lg">
                            <div class="flex items-center justify-between">
                                <div>
                                    <h4 class="font-semibold mb-2">内存碎片率判断标准</h4>
                                    <ul class="text-sm text-gray-700 space-y-1">
                                        <li><i class="fas fa-check text-green-500 mr-2"></i>1.0 - 1.5：正常范围</li>
                                        <li><i class="fas fa-exclamation text-yellow-500 mr-2"></i>&gt; 1.5：存在碎片问题</li>
                                        <li><i class="fas fa-times text-red-500 mr-2"></i>&lt; 1.0：可能存在内存交换</li>
                                    </ul>
                                </div>
                                <i class="fas fa-puzzle-piece text-5xl text-purple-300 opacity-50"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Step 5 -->
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8 card-hover">
                <div class="flex items-start">
                    <div class="step-number mr-6">5</div>
                    <div class="flex-1">
                        <h3 class="text-xl font-bold mb-4">监控缓存数据</h3>
                        <p class="text-gray-700 mb-4">
                            检查键的过期时间设置，确保合理的数据生命周期管理：
                        </p>
                        <div class="command-box">TTL &lt;key&gt;</div>
                        <div class="command-box">CONFIG GET maxmemory-policy</div>
                        <div class="mt-6 grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div